package com.yipao.wx.web.notice;

import com.alibaba.fastjson.JSONObject;
import com.yipao.core.util.ResponseUtil;
import com.yipao.db.service.order.OrderService;
import com.yipao.db.util.OrderUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 安卓中控端通知
 *
 * @author tianhao
 */
@RestController
@RequestMapping("/wx/dev/notice")
@Validated
@Slf4j
public class WxDevNoticeController {
    @Resource
    private OrderService orderService;

    /**
     * 订单通知
     */
    @PostMapping("order")
    public Object orderNotice(@RequestBody JSONObject jsonObject) {
        Integer orderId = jsonObject.getInteger("orderId");
        String type = jsonObject.getString("type");
        if (StringUtils.isEmpty(orderId) || StringUtils.isEmpty(type)) {
            log.warn("安卓中控的订单通知,参数为空");
            return ResponseUtil.fail(ResponseUtil.STATUS_CODE_BAD_ARGUMENT, "参数为空");
        }

        // 订单状态
        Short orderStatus;
        switch (type) {
            case "3": {
                // 制作完成通知
                orderStatus = OrderUtil.STATUS_PRODUCTION_COMPLETE;
                break;
            }
            case "4": {
                // 取货通知
                orderStatus = OrderUtil.STATUS_CONFIRM;
                break;
            }
            default:
                return ResponseUtil.fail(ResponseUtil.STATUS_CODE_BAD_ARGUMENT, "参数type值不匹配");
        }
        orderService.updateOrderStatus(orderId, orderStatus);
        return ResponseUtil.ok();
    }
}